using System;
using System.Collections.Generic;
using System.Text;
using FireStarter.Base;
using FireStarter.CodeGeneration;
using FireStarter.CodeGeneration.Generator;
using FireStarter.CodeGeneration.Parser;
using FireStarterModeller.BO;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration.Templates.UsingControlManager
{
    [TestFixture]
    public class Test_UI_ParentChildControl_UsingControlManager : TestBase
    {
        //TODO: what if a null DMClass is passed through?

        [Ignore("TODO: fix the template names")]
        [Test]
        public void Test_OneBusinessObjectOneProperty()
        {
            //---------------Set up test pack-------------------
            string expectedTemplate =
                @"using Habanero.BO;
using Habanero.Faces.Base;
using TestProject.BO;

namespace TestProject.UI
{
    public class CustomerOrdersAndOrderItemsControl : ControlManager
    {
        private IReadOnlyGridControl _customerOrdersGrid;
        private IReadOnlyGridControl _orderItemsGrid;

        public CustomerOrdersAndOrderItemsControl(IControlFactory controlFactory) : base(controlFactory)
        {
        }

        protected override void InitialiseControl()
        {
            SetupGrids();
            AddGridFilters();
            LoadCustomerOrdersCollection();
        }

        private void SetupGrids()
        {
            _customerOrdersGrid = _controlFactory.CreateReadOnlyGridControl();
            _orderItemsGrid = _controlFactory.CreateReadOnlyGridControl();
            ISplitter splitter = _controlFactory.CreateSplitter();

            _orderItemsGrid.Dock = DockStyle.Fill;
            _customerOrdersGrid.Dock = DockStyle.Top;
            splitter.Dock = DockStyle.Top;

            _customerOrdersGrid.Height = 200;

            Control.Controls.Add(_orderItemsGrid);
            Control.Controls.Add(splitter); 
            Control.Controls.Add(_customerOrdersGrid);

            _customerOrdersGrid.Grid.BusinessObjectSelected += delegate { SetOrderItemsCollection(); };
        }

        private void AddGridFilters()
        {
            _customerOrdersGrid.FilterControl.Visible = false;
            _orderItemsGrid.FilterControl.Visible = false;
            //TODO: Add the filters you want and remove the above lines
        }

        private void LoadCustomerOrdersCollection()
        {
            BusinessObjectCollection<CustomerOrder> customerOrders = new BusinessObjectCollection<CustomerOrder>();
            customerOrders.LoadAll();
            _customerOrdersGrid.SetBusinessObjectCollection(customerOrders);
        }

        private void SetOrderItemsCollection()
        {
            CustomerOrder selectedCustomerOrder = (CustomerOrder)_customerOrdersGrid.SelectedBusinessObject;
            if (selectedCustomerOrder == null) return;
            _orderItemsGrid.SetBusinessObjectCollection(selectedCustomerOrder.OrderItems);
        }
    }
}";

            string template = FireStarter.CodeGeneration.Templates.UI_ParentChildControl_UsingControlManager;

            ModelParentChildControl parentChildControl = new ModelParentChildControl();
            parentChildControl.ParentClass = "CustomerOrder";
            parentChildControl.ChildRelationshipName = "OrderItems";

            StatementParser statementParser = new StatementParser();
            IModelSolution modelSolution = new DMSolution();
            modelSolution.BOProjectName = "TestProject.BO";
            modelSolution.UIProjectName = "TestProject.UI";
            
            parentChildControl.Parent = modelSolution;

            ModelStrategyMapper modelStrategyMapper = new ModelStrategyMapper();
            modelStrategyMapper.SetStrategy(typeof(IModelElement), new ReflectionGenerationStrategy());
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            TemplateExpression templateExpression = statementParser.Parse(ref template);
            string generatedTemplate = templateExpression.Generate(modelStrategyMapper, parentChildControl);
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedTemplate, generatedTemplate);
        }
    }

    internal class ModelParentChildControl : IModelElement
    {
        public string ParentClass { get; set; }
        public string ChildRelationshipName { get; set; }
        public IModelElement Parent { get; set; }
    }
}
